//
//  ShopCartViewController.m
//  groupProject
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "ShopCartViewController.h"
#import "Config.h"
#import "ShopCarTableViewCell.h"
#import "Goods.h"
#import "group.h"
#import "NullViewController.h"

@interface ShopCartViewController ()

@end

@implementation ShopCartViewController {
    BOOL isSelect;
    BOOL isEdit;
    BOOL isSelectAll;
    ShopCarTableViewCell *Cell;
    Goods *myGoods;
    group * myGroup;
    NullViewController * nullVC;
    UIButton * selectAll;
}
@synthesize SCTableView;

- (void)viewDidLoad {
    myGroup = [[group alloc]init];
    nullVC = [[NullViewController alloc]init];
//    if (myGroup.goodsGroup.count == 0) {
//        [self.navigationController pushViewController:nullVC animated:YES];
//    }
//    [super viewDidLoad];
    
   
    isSelectAll = YES;
    isSelect = YES;
    isEdit = NO;
    self.navigationItem.title = @"购物车";
    
    SCTableView = [[UITableView alloc] init];
    SCTableView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, 500);
    SCTableView.delegate = self;
    SCTableView.dataSource = self;
    SCTableView.rowHeight=80;
    //SCCell = [[ShopCarTableViewCell alloc] init];
    
    [self.view addSubview:SCTableView];
    
//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit_BtnClick)];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit_BtnClick)];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,NAVIGATION_BAR_HEIGHT+500, SCREEN_WIDTH,SCREEN_HEIGHT-NAVIGATION_BAR_HEIGHT-500-TABBAR_HEIGHT )];
    [view setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:view];
    //全选按钮
    UILabel * selectAllLable = [[UILabel alloc] initWithFrame:CGRectMake(SPACING *4,SPACING,40,40)];
    selectAllLable.text = @"全选";
    [view addSubview:selectAllLable];
    selectAll = [[UIButton alloc] initWithFrame:CGRectMake(SPACING, SPACING, SPACING*2, SPACING*2)];
    [selectAll setBackgroundImage:[UIImage imageNamed:@"selectButton.png"] forState:UIControlStateNormal];
    [selectAll addTarget:self action:@selector(selectAll_Btn) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:selectAll];
    //总计
    UILabel * total = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 40, 40)];
    total.text = @"总计:";
    [view addSubview:total];
    
    UILabel * totalPrice = [[UILabel alloc] initWithFrame:CGRectMake(140, SPACING,100, 40)];
    totalPrice.text = @" ";
    [view addSubview:totalPrice];
    
    UIButton * payGoods = [[UIButton alloc] initWithFrame:CGRectMake(250, SPACING, 75, 50)];
    [payGoods setTitle:@"去结算" forState:UIControlStateNormal];
//    [payGoods addTarget:self action:@selector(alipy_BtnClick) forControlEvents:UIControlEventTouchUpInside];
    [payGoods setBackgroundColor:[UIColor redColor]];
    [view addSubview:payGoods];
    
    
}


- (void)selectAll_Btn {
    if (isSelectAll) {
        [selectAll setImage:nil forState:UIControlStateNormal];
        isSelectAll = NO;
    } else {
         [selectAll setImage:[UIImage imageNamed:@"selectButton.png"] forState:UIControlStateNormal];
        isSelectAll = YES;
    }
//    for (<#initialization#>; <#condition#>; <#increment#>) {
//        <#statements#>
//    }
}
- (void)edit_BtnClick {
    isEdit = YES;
    [SCTableView setEditing:!SCTableView.editing animated:YES];
}
//返回编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (isEdit == YES) {
        return UITableViewCellEditingStyleDelete;
    } else {
    return UITableViewCellEditingStyleNone;
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        
        //删除原始数据
//        Group *tmp=self.data[indexPath.section];
//        [tmp.contacts removeObjectAtIndex:indexPath.row];
        
        //刷新表单
        //[myTableView reloadData];
        
        //局部刷新
//        [myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
//代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    Group *tmp=self.data[section];
//    return tmp.contacts.count;
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString * SCTableView1= @"SCTableView";
 Cell = [tableView dequeueReusableCellWithIdentifier:SCTableView1];
   
    if (Cell == nil) {
        Cell = [[ShopCarTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SCTableView1];
    }
        myGoods = [[Goods alloc]init];
    myGoods.currentAmuont = 1;
    if ([Cell.currentAmount.text  isEqual: @"1"]) {
        Cell.ReduceButton.userInteractionEnabled=NO;
        Cell.ReduceButton.alpha=0.4;
    }
        [Cell.selectButton addTarget:self action:@selector(Select_BtnClick) forControlEvents:UIControlEventTouchUpInside];
        [Cell.ReduceButton addTarget:self action:@selector(Reduce_BtnClick) forControlEvents:UIControlEventTouchUpInside];
        [Cell.AddButton addTarget:self action:@selector(Add_BtnClick) forControlEvents:UIControlEventTouchUpInside];
    return Cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
//全选按钮
//-(void)selectBtnClick:(UIButton *)sender
//{
//    //判断是否选中，是改成否，否改成是，改变图片状态
//    sender.tag = !sender.tag;
//    if (sender.tag)
//    {
//        [sender setImage:[UIImage imageNamed:@"复选框-选中.png"] forState:UIControlStateNormal];
//        
//    }else{
//        [sender setImage:[UIImage imageNamed:@"复选框-未选中.png"] forState:UIControlStateNormal];
//    }
//    //改变单元格选中状态
//    for (int i=0; i<infoArr.count; i++)
//    {
//        GoodsInfoModel *model = [infoArr objectAtIndex:i];
//        model.selectState = sender.tag;
//    }
//    //计算价格
//    [self totalPrice];
//    //刷新表格
//    [_MyTableView reloadData];
//    
//}
//计算总价
//-(void)totalPrice
//{
//    //遍历整个数据源，然后判断如果是选中的商品，就计算价格（单价 * 商品数量）
//    for ( int i =0; i<infoArr.count; i++)
//    {
//        GoodsInfoModel *model = [infoArr objectAtIndex:i];
//        if (model.selectState)
//        {
//            allPrice = allPrice + model.goodsNum *[model.goodsPrice intValue];
//        }
//    }
//    
//    //给总价文本赋值
//    _allPriceLab.text = [NSString stringWithFormat:@"%.2f",allPrice];
//    NSLog(@"%f",allPrice);
//    
//    //每次算完要重置为0，因为每次的都是全部循环算一遍
//    allPrice = 0.0;
//}
//减
- (void)Reduce_BtnClick {

        myGoods.currentAmuont--;
    myGoods.currentPrice = myGoods.currentAmuont * myGoods.onePrice;
    Cell.PriceLable.text = [NSString stringWithFormat:@"%ld",(long)myGoods.currentPrice];
        if (myGoods.currentAmuont == 1) {
            Cell.ReduceButton.userInteractionEnabled=NO;
            Cell.ReduceButton.alpha=0.4;
        }
            Cell.currentAmount.text = [NSString stringWithFormat: @"%ld", (long)myGoods.currentAmuont];
}
- (void)Add_BtnClick {
    Cell.ReduceButton.userInteractionEnabled=YES;
    Cell.ReduceButton.alpha=1;
    myGoods.currentAmuont++;
    myGoods.currentPrice = myGoods.currentAmuont * myGoods.onePrice;
    Cell.PriceLable.text = [NSString stringWithFormat:@"%ld",(long)myGoods.currentPrice];
    Cell.currentAmount.text = [NSString stringWithFormat: @"%ld", (long)myGoods.currentAmuont];
}
//选中按钮
- (void)Select_BtnClick {
    
    if (isSelect) {
        [ Cell.selectButton setBackgroundImage:nil forState:UIControlStateNormal];
        isSelect = NO;
//        NSLog(@"1");
    } else {
        [ Cell.selectButton setBackgroundImage:[UIImage imageNamed:@"selectButton.png"] forState:UIControlStateNormal];
        isSelect = YES;
   
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
